package com.mathslogica;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URL;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.gdata.client.youtube.YouTubeService;
import com.google.gdata.data.youtube.PlaylistLinkEntry;
import com.google.gdata.data.youtube.PlaylistLinkFeed;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;

@SuppressWarnings("serial")
public class MathslogicaServlet extends HttpServlet {

	@Override
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		YouTubeService service = new YouTubeService(MLConstants.CLIENT_ID,
				MLConstants.APP_KEY);
		try {
			service.setUserCredentials("moazzam.cricket@gmail.com",
					"indiaisthebest12!@");

			String feedUrl = "http://gdata.youtube.com/feeds/api/users/khanacademy/playlists";
			// VideoFeed videoFeed = service.getFeed(new URL(feedUrl),
			// VideoFeed.class);

			PlaylistLinkFeed feed = service.getFeed(new URL(feedUrl),
					PlaylistLinkFeed.class);
			for (PlaylistLinkEntry entry : feed.getEntries()) {

				
				System.out.println("Title: " + entry.getTitle().getPlainText());
				System.out.println("Description: "
						+ entry.getSummary().getPlainText());
				System.out
						.println("Number of entries: " + entry.getCountHint());
			}

		} catch (AuthenticationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {

		UserService userService = UserServiceFactory.getUserService();
		User user = userService.getCurrentUser();

		if (user != null
				&& (user.getEmail().equals("moazzam.cricket@gmail.com") || user
						.getEmail().equals("deepakdub@gmail.com"))) {
			Key key = KeyFactory.createKey("VideoData",
					req.getParameter(MLConstants.ID));

			Entity videoEntity = new Entity("VideoLecture", key);
			videoEntity.setProperty("id", key.getName());

			videoEntity.setProperty("transcript",
					req.getParameter(MLConstants.TRANSCRIPT));

			DatastoreService datastore = DatastoreServiceFactory
					.getDatastoreService();
			datastore.put(videoEntity);

			PrintWriter out = resp.getWriter();
			out.write("Done\n");
			out.close();

		} else {
			resp.sendRedirect("/jsp/update.jsp");
		}

		// resp.sendRedirect("/guestbook.jsp?guestbookName=" + guestbookName);
	}
}
